﻿using System;
using System.Net;
using System.Net.Sockets;

namespace B2BCore
{
    /// <summary>
    /// Represents a single user and stores that users information
    /// </summary>
    public class User
    {
        private System.Net.IPAddress _ipAddress;
        private System.Net.Sockets.Socket _socket;

        /// <summary>
        /// User
        /// </summary>
        public User()
        {
            Name = String.Empty;
            _ipAddress = System.Net.IPAddress.None;
            _socket = null;
        }

        /// <summary>
        /// Represents a single user
        /// </summary>
        /// <param name="username">The name of the user</param>
        /// <param name="socket">The users socket</param>
        public User(String username, Socket socket)
            : this(username, socket, String.Empty)
        {
            if (socket == null)
                throw new ArgumentNullException(Utilities.GetVariableName(socket));

            if (String.IsNullOrEmpty(username))
                throw new ArgumentException(ErrorStrings.ExMsgUserNameNullOrEmpty, Utilities.GetVariableName(username));
        }

        /// <summary>
        /// Represents a single user
        /// </summary>
        /// <param name="username">The name of the user</param>
        /// <param name="socket">The users socket</param>
        /// <param name="ipAddress">The IP address of the user</param>
        public User(String username, Socket socket, String ipAddress)
        {
            if (socket == null)
                throw new ArgumentNullException(Utilities.GetVariableName(socket));

            if (String.IsNullOrEmpty(username))
                throw new ArgumentException(ErrorStrings.ExMsgUserNameNullOrEmpty, Utilities.GetVariableName(username));

            Name = username;
            _socket = socket;

            if (!IPAddress.TryParse(ipAddress, out _ipAddress))
                _ipAddress = IPAddress.None;
        }

        /// <summary>
        /// Represents a single user
        /// </summary>
        /// <param name="username">The name of the user</param>
        /// <param name="socket">The users socket</param>
        /// <param name="ipAddress">The IP address of the user</param>
        public User(String username, Socket socket, IPAddress ipAddress)
        {
            if (socket == null)
                throw new ArgumentNullException(Utilities.GetVariableName(socket));
            if (String.IsNullOrEmpty(username))
                throw new ArgumentException(ErrorStrings.ExMsgUserNameNullOrEmpty, Utilities.GetVariableName(username));
            if (ipAddress == null)
                throw new ArgumentNullException(Utilities.GetVariableName(ipAddress), ErrorStrings.ExMsgIpAddressNull);

            Name = username;
            _socket = socket;
            _ipAddress = ipAddress;
        }

        #region Properties

        /// <summary>
        /// Get or sets the users name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the users IP Address
        /// </summary>
        public IPAddress IpAddress
        {
            get { return _ipAddress; }
            set { _ipAddress = value; }
        }

        #endregion

        #region Functions
        /// <summary>
        /// Check to see if the user has an ip address
        /// </summary>
        /// <returns></returns>
        public bool HasIPAddress()
        {
            if (_ipAddress == System.Net.IPAddress.None)
                return false;

            return true;
        }
        #endregion
    }

    internal class LocalUser : User
    {
        public LocalUser()
        {
            Name = System.Environment.UserName;
            IpAddress = Utilities.GetLocalIPAddress();
        }
            
    }
}
